//
//  BBuyNowVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/9.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BBuyNowVC.h"
#import "BBuyNowShopHeaderView.h"
#import "BBuyNowShopCell.h"
#import "BShopPartModel.h"

#import "BBuyNowRecommendVC.h"//最新上架、好物优选
#import "BBuyNowShopDetailVC.h"//产品详情

@interface BBuyNowVC () <UITableViewDelegate,UITableViewDataSource,BBuyNowShopHeaderViewDeletate>
{
    BOOL _isRequesting;//是否正在请求数据
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *shopList;//产品列表
@property (nonatomic, assign) NSInteger pageNumber;;
@end

@implementation BBuyNowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我要预订";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = kColorWithHex(0xffffff);
    _shopList = [NSMutableArray array];
    
    [self setupViewAction];
}
#pragma mark - 界面布局方法
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 300;
    
    [_tableView registerClass:[BBuyNowShopCell class] forCellReuseIdentifier:NSStringFromClass([BBuyNowShopCell class])];
    
    BBuyNowShopHeaderView *viewHeader = [[BBuyNowShopHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScaleNum(180) + 52)];
    viewHeader.delegate = self;
    _tableView.tableHeaderView = viewHeader;
    
    kWeakSelf;
    //下拉刷新
    MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNumber = 1;
        [weakSelf requestData];
    }];
    mj_header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.mj_header = mj_header;
    
    //上拉加载
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    
    [_tableView.mj_header beginRefreshing];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shopList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BBuyNowShopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BBuyNowShopCell class]) forIndexPath:indexPath];
    BShopPartModel *model = [self.shopList objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BShopPartModel *model = [self.shopList objectAtIndexSafe:indexPath.row];
    BBuyNowShopDetailVC *vc = [BBuyNowShopDetailVC new];
    vc.shopID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - BBuyNowShopHeaderViewDeletate
- (void)buyNowShopHeaderViewClick:(BBuyNowShopHeaderViewCategoryType)type{
    BBuyNowRecommendVC *vc = [[BBuyNowRecommendVC alloc] init];
    if (type == BBuyNowShopHeaderViewCategoryTypeNew) {
        //最新上架
        vc.type = RecommendVCTypeNew;
    }else if (type == BBuyNowShopHeaderViewCategoryTypeBest){
        //好物优选
        vc.type = RecommendVCTypeBest;
    }
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 数据请求
- (void)requestData{
    if (_isRequesting) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        return;
    }
    _isRequesting = YES;
    [MBProgressHUD showHUDAddedTo:self.view msg:@"数据加载中" animated:YES];
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObjectIfNotNil:@"1" forKey:@"type"];
    [param setObject:@(_pageNumber) forKey:@"pageNum"];
    [NetWorkTool POST:getPartsList param:param success:^(id dic) {
        [weakSelf requestResult];
        
        NSArray *dataArr = [dic objectForKeyNotNil:@"data"];
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BShopPartModel *model = [BShopPartModel mj_objectWithKeyValues:obj];
            [weakSelf.shopList addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
        weakSelf.pageNumber++;
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf requestResult];
    } fail:^(NSError *error) {
        [weakSelf requestResult];
    } needUser:YES];
}
- (void)requestResult{
    _isRequesting = NO;
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if (self.pageNumber == 1) {
        [self.shopList removeAllObjects];
    }
}

@end
